const express = require('express')
const router = express.Router()
const db = require('../../connection/Connection')
const jwt = require('jsonwebtoken')
const SECRET = 'asdfta219*&%^jWJSHFJ'


router.post('/getStationInfo', async (req, res) => {
    //token
    const result = jwt.verify(req.body.token, SECRET)
    let sql = 'select * from user where username = ?'
    db.base(sql, result.username, response => {

        //验证成功则判定是否存在选择条件stationName
        if (response.length > 0 ) {
            //为空或未选择则返回所有的数据与选项
            if (req.body.stationName === null || req.body.stationName === '未选择') {
                let sql = 'select * from stationInfo'
                db.base(sql, null, response => {
                    // console.log(response)
                    //处理选项
                    let options = []
                    let n = []

                    //处理数据
                    let data = []
                    for ( let i = 0; i < response.length; i++) {
                        let clock_in_1 = parseInt(response[i].clock_in / 60)
                        let clock_in_2 = response[i].clock_in - clock_in_1 * 60
                        if (clock_in_2 === 0) clock_in_2 = '00'
                        let clock_out_1 =parseInt(response[i].clock_out / 60)
                        let clock_out_2 = response[i].clock_out - clock_out_1 * 60
                        if (clock_out_2 === 0) clock_out_2 = '00'
                        data[i] = {
                            'stationName': response[i].station_name,
                            'clockIn': clock_in_1 + ':' + clock_in_2,
                            'clockOut': clock_out_1+ ':' + clock_out_2,
                            'averageSalary': response[i].average_salary,
                            'jobNumber': response[i].number_of_jobs,
                            'department': response[i].department_name + '(' + response[i].region + ')',
                            'department_id': response[i].department_id,
                            'station_id': response[i].station_id
                        }
                        if (n.indexOf(data[i].department + '-' + data[i].department_id) === -1)
                            n.push(data[i].department + '-' + data[i].department_id)

                    }
                    //获取department数据
                    let sql = 'select * from departmentInfo_nostation'
                    db.base(sql, null, response => {
                        for (let j = 0; j < response.length; j++) {
                            options[j] = {
                                'value': response[j].department_id,
                                'label': response[j].department_name + '(' + response[j].region + ')'
                            }
                        }
                        options[response.length] = {
                            'value': 'non_choice',
                            'label': '未选择'
                        }
                        console.log(options)
                        res.json({
                            'data': data,
                            'option': options
                        })
                    })




                    // console.log(n)
                    // console.log(data)

                })
            }
            //否则根据stationName选择数据
            else {
                // console.log(req.body.stationName)
                //处理传送来的部门与地区
                let department = req.body.stationName.split('(')
                let region = department[1].split(')')
                // console.log(department[0])
                // console.log(region[0])
                let data = [department[0],region[0]]
                let sql = 'select * from stationInfo where department_name = ? and region = ?'
                db.base(sql, data, response => {
                    console.log(response)
                    let data = []
                    for ( let i = 0; i < response.length; i++) {
                        let clock_in_1 = parseInt(response[i].clock_in / 60)
                        let clock_in_2 = response[i].clock_in - clock_in_1 * 60
                        if (clock_in_2 === 0) clock_in_2 = '00'
                        let clock_out_1 = parseInt(response[i].clock_out / 60)
                        let clock_out_2 = response[i].clock_out - clock_out_1 * 60
                        if (clock_out_2 === 0) clock_out_2 = '00'
                        data[i] = {
                            'stationName': response[i].station_name,
                            'clockIn': clock_in_1 + ':' + clock_in_2,
                            'clockOut': clock_out_1 + ':' + clock_out_2,
                            'averageSalary': response[i].average_salary,
                            'jobNumber': response[i].number_of_jobs,
                            'department': response[i].department_name + '(' + response[i].region + ')',
                            'station_id': response[i].station_id
                        }
                    }
                    res.json({ data })
                })
            }

        }
    })
})

module.exports = router
